package com.zqq.framedagger.ui.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.alibaba.android.arouter.launcher.ARouter
import com.zhuanju.core.utils.ZLog
import com.zqq.baselibrary.ext.loadUrl
import com.zqq.baselibrary.ext.onClick
import com.zqq.baselibrary.ui.fragment.BaseFragment
import com.zqq.baselibrary.utils.AppPrefsUtils
import com.zqq.framedagger.R
import com.zqq.framedagger.ui.activity.SettingActivity
import com.zqq.provider.common.ProviderConstant
import com.zqq.provider.common.afterLogin
import com.zqq.provider.common.isLogined
import com.zqq.provider.router.RouterPath
import com.zqq.user.ui.activity.UserInfoActivity
import com.zxx.order.common.OrderConstant
import com.zxx.order.common.OrderStatus
import kotlinx.android.synthetic.main.fragment_me.*
import org.jetbrains.anko.support.v4.startActivity
import org.jetbrains.anko.support.v4.toast

/**
 * 五、"我的"界面
 * wc
 * @author zqq on 2018/7/30
 */
class MeFragment : BaseFragment(), View.OnClickListener {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        super.onCreateView(inflater, container, savedInstanceState)
        return inflater.inflate(R.layout.fragment_me, null)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView()
    }

    override fun onStart() {
        super.onStart()
        ZLog.e("--onStart-")
        loadData()
    }

    /**
     * 加载初始数据
     */
    private fun loadData() {
        if (isLogined()) {
            val userIcon = AppPrefsUtils.getString(ProviderConstant.KEY_SP_USER_ICON)
            if (userIcon.isNotEmpty()) {
                mUserIconIv.loadUrl(userIcon)
            }
            mUserNameTv.text = AppPrefsUtils.getString(ProviderConstant.KEY_SP_USER_NAME)
        } else {
            mUserIconIv.setImageResource(R.mipmap.icon_default_user)
            mUserNameTv.text = getString(R.string.un_login_text)
        }
    }

    /**
     * 初始化视图
     */
    private fun initView() {
        mUserIconIv.onClick(this)
        mUserNameTv.onClick(this)

        mWaitPayOrderTv.onClick(this)
        mWaitConfirmOrderTv.onClick(this)
        mCompleteOrderTv.onClick(this)
        mAllOrderTv.onClick(this)

        mAddressTv.onClick(this)
        mShareTv.onClick(this)
        mSettingTv.onClick(this)
    }

    //点击事件
    override fun onClick(v: View) {
        when (v.id) {
            R.id.mUserIconIv, R.id.mUserNameTv -> {
                afterLogin {
                    startActivity<UserInfoActivity>()   //anko库包
                }
//                ARouter.getInstance()
//                        .build(RouterPath.UserCenter.PATH_LOGIN)
//                        .withInt(ProviderConstant.KEY_ORDER_ID, 555)
//                        .navigation()
            }

            //订单状态
            R.id.mWaitPayOrderTv -> {
                //传递参数值为Int的，key为前面这个参数
                //startActivity<OrderActivity>(OrderConstant.KEY_ORDER_STATUS to OrderStatus.ORDER_WAIT_PAY)

                ARouter.getInstance()
                        .build(RouterPath.OrderCenter.PATH_ORDER_STATUS)
                        .withInt(OrderConstant.KEY_ORDER_STATUS, OrderStatus.ORDER_WAIT_PAY)
                        .navigation()
            }
            R.id.mWaitConfirmOrderTv -> {
                ARouter.getInstance()
                        .build(RouterPath.OrderCenter.PATH_ORDER_STATUS)
                        .withInt(OrderConstant.KEY_ORDER_STATUS, OrderStatus.ORDER_WAIT_CONFIRM)
                        .navigation()
            }
            R.id.mCompleteOrderTv -> {
                ARouter.getInstance()
                        .build(RouterPath.OrderCenter.PATH_ORDER_STATUS)
                        .withInt(OrderConstant.KEY_ORDER_STATUS, OrderStatus.ORDER_COMPLETED)
                        .navigation()
            }
            R.id.mAllOrderTv -> {
                afterLogin {
                    ARouter.getInstance()
                            .build(RouterPath.OrderCenter.PATH_ORDER_STATUS)
                            .navigation()
                }
            }

            //设置地址
            R.id.mAddressTv -> {
                afterLogin {
                    ARouter.getInstance()
                            .build(RouterPath.OrderCenter.PATH_SHIP_ADDRESS)
                            .navigation()
                }
            }
            R.id.mShareTv -> {
                toast(R.string.coming_soon_tip)
            }
            R.id.mSettingTv -> {
                startActivity<SettingActivity>()
            }
        }
    }

}